<?php


namespace app\community\controller;



use app\common\controller\Common;
use app\common\controller\Together;
use app\index\controller\Base;
use think\Config;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Article extends Base
{
    //文章管理
    public function lists(){
        $common = new Common();
        //获取管理的景区
        $Together = new Together();
        $colony = $Together->findColony();
        $this->assign("colony",$colony);
        //获取导航
        if(empty($colony)){
            $colonyId = 0;
        }else{
            $colonyId = empty($_GET['colonyId'])?$colony[0]['id']:$_GET['colonyId'];
        }

        $where['colony_id'] = $colonyId;
        $where['status'] = 1;
        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        $nav = $common->public_select("community_nav",$where,"id,name")->select();
        //获取导航下的栏目
        foreach ($nav as $k=>$v){
            $classify = $common->public_select("community_article_classify",['nav_id'=>$v['id'],'pid'=>0],"id,name,type")->select();
            foreach ($classify as $k1=>$v1){
                //组装子栏目
                $subtopic = $common->public_select("community_article_classify",['pid'=>$v1['id']],"id,name,type")->select();
                $classify[$k1]['subtopic'] = $subtopic;
            }
            $nav[$k]['classify'] = $classify;
        }
        $this->assign("colonyId",$colonyId);
        $this->assign("nav",$nav);
        $list = '';
        $this->assign("list",$list);
        return $this->fetch('lists');
    }

    //添加修改文章视图
    public function addLists(){
        $common = new Common();
        $config = $common->selconfig();
        $colonyId = empty($_GET['colony_id'])?0:$_GET['colony_id'];
        $classify_id = empty($_GET['classify_id'])?0:$_GET['classify_id'];
        if(!empty($classify_id)){
            $colonyId = $common->public_select("community_article_classify",['id'=>$classify_id,'type'=>['neq',2]])->value("colony_id");
        }
        //详细信息
        if(!empty($_GET['id'])){
            $id = $_GET['id'];
            $detail =  Db::connect($config)->name("admin_content")->alias("a")
                ->join("community_article_classify b","a.classify_id = b.id")
                ->where(['a.id'=>$id])
                ->field("a.*,b.colony_id,b.nav_id")->find();
            $detail['thumb'] = empty($detail['thumb'])?"":$detail['thumb'];
            $classify_id = $detail['classify_id'];
            $colonyId = $detail['colony_id'];
            $this->assign("detail",$detail);

            $images = explode(",",$detail['images']);

            foreach ($images as $k=>$v){
                if(empty($v)){
                    unset($images[$k]);
                }
            }
            $this->assign("images",$images);
        }
        //获取导航信息
        $nav = $common->public_select("community_nav",['colony_id'=>$colonyId],"id,name")->select();
        //获取第一行栏目
        if(!empty($classify_id)){
            $nav_id = $common->public_select("community_article_classify",['id'=>$classify_id,'type'=>['not in',[2,8]]])->value("nav_id");
        }else{
            $nav_id = empty($nav)?0:$nav[0]['id'];
        }
        $classify = $common->public_select("community_article_classify",['nav_id'=>$nav_id,'pid'=>0,'type'=>['not in',[2,8]]],"id,name,type")->select();
        //获取下级栏目
        foreach ($classify as $k=>$v){
            $classify[$k]['child'] = $common->public_select("community_article_classify",['pid'=>$v['id'],'type'=>['not in',[2,8]]],"id,name,type")->select();
        }
        if(empty($classify_id)){
            $type = empty($classify[0]['type'])?1:$classify[0]['type'];
        }else{
            $type = $common->public_select("community_article_classify",['id'=>$classify_id])->value("type");
        }

        $this->assign("nav_id",$nav_id);
        $this->assign("colony_id",$colonyId);
        $this->assign("classify_id",$classify_id);
        $this->assign("type",$type);
        $this->assign("nav",$nav);
        $this->assign("classify",$classify);
        if(empty($_GET['id'])){
//            if($type==2){
//                $record = $this->record();
//                $this->assign($record);
//                return $this->fetch('addData');
//            }
            return $this->fetch('addLists');
        }else{
            return $this->fetch('editLists');
        }
    }


    //添加修改文章操作
    public function doLists(){
        $common = new Common();
        Db::startTrans();
        try{
            /*$rule = [
                ["classify_id", "require", "请选择栏目"],
                ["title", "require", "请输入标题"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }*/
            if(empty($_POST['classify_id'])){
                throw new Exception("请选择栏目");
            }
            $classify_id = $_POST['classify_id'];
            //查询所属模型
            $type = $common->public_select("community_article_classify",['id'=>$classify_id])->value("type");

            if(empty($_POST['title'])){
                throw new Exception("请输入标题");
            }
            $thisWechat = Session::get("thisWechat");
            $data['uniacid'] = $thisWechat['id'];
            //公有数据
            $data['classify_id'] = $_POST['classify_id'];
            $data['title'] = $_POST['title'];
//            $data['address'] = $_POST['address'];
//            $data['hot'] = $_POST['hot'];
            $data['thumb'] = $_POST['thumb'];
            $data['desc'] = $_POST['desc'];
            $data['sort'] = $_POST['sort'];
            $data['status'] = $_POST['status'];
            $data['Wdate'] = empty($_POST['Wdate'])?date("Y-m-d",time()):$_POST['Wdate'];
            $data['author'] = empty($_POST['author'])?Session::get("user_name"):$_POST['author'];
            $data['source'] = $_POST['source'];

            if(!empty($_POST['images'])){
                $images = $_POST['images'];
                $data['images'] = implode($images,",");
            }

            //数据拼装
            if($type==1 || $type==7 || $type==9){
                if(empty($_POST['content'])){
                    throw new Exception("内容不能为空");
                }
                $data['content'] = $_POST['content'];
                $data['path'] = $_POST['path'];
            }else if($type == 3){
//                if(empty($_POST['link'])){
//                    throw new Exception("链接地址不能为空");
//                }
                $data['link'] = $_POST['link'];
            }else if($type == 5){
                if(empty($_POST['mobile'])){
                    throw new Exception("电话号码不能为空");
                }
                $data['mobile'] = $_POST['mobile'];
            }else if($type == 4){
                if(empty($_POST['start_time'])){
                    throw new Exception("开始时间不能为空");
                }
                if(empty($_POST['end_time'])){
                    throw new Exception("结束时间不能为空");
                }
                if(empty($_POST['content'])){
                    throw new Exception("内容不能为空");
                }
                $data['content'] = $_POST['content'];
                $data['path'] = $_POST['path'];
                $data['integral'] = $_POST['integral'];
                $data['start_time'] = strtotime($_POST['start_time']);
                $data['end_time'] = strtotime($_POST['end_time']);
            }else{

            }
            if(!empty($_POST['id'])){
                $id = $data['id'] = $_POST['id'];
                //修改操作
                $common->handle("admin_content",$data);
                $log['remark'] = "用户【" . Session::get("user_name") . "】更新内容,内容id【".$id."】";
                $common->addLog($log);
            }else{
                //添加操作
                $id = $common->handle("admin_content",$data);
                $log['remark'] = "用户【" . Session::get("user_name") . "】添加新内容,内容id【".$id."】";
                $common->addLog($log);
            }
            $black['classify_id'] = $classify_id;
            $black['code'] = 0;
            $black['msg'] = "添加成功";
            Db::commit();
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
            Db::rollback();
        }
        return $black;
    }

    //删除文章
    public function delLists(){
        Db::startTrans();
        try{
            $common = new Common();
            $id = $_POST['id'];
            $navList = $common->public_select("admin_content",['id'=>['in',$id]],"id,title")->select();
            $navList = array_column($navList,"id");

            $navList = implode(",",$navList);

            $log['remark'] = "账号【" . Session::get("user_name") . "】删除内容【".$navList."】";
            $common->addLog($log);
            $common->public_select("admin_content",['id'=>['in',$id]])->delete();
            Db::commit();
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //分类管理
    public function index(){
        $common = new Common();

        //权限查询
        $role_id = $common->thispower();
        $powerList = $common->showpower($role_id);
        $this->assign("powerList",$powerList);

        //获取景区列表
        $Together = new Together();
        $community = $Together->findColony();
        $this->assign("community",$community);
        $communityWhere = array_column($community,"id");
        $where['a.colony_id'] = ['in',$communityWhere];
        $name = $propertyName = $status = $colony_id ="";
        if(!empty($_GET['name'])){
            $name = $_GET['name'];
            $where['a.name'] = ['like',"%{$name}%"];
        }
        if(!empty($_GET['propertyName'])){
            $propertyName = $_GET['propertyName'];
            $navWhere['colony_id'] = ['in',$communityWhere];
            $navWhere['name'] = ['like',"%{$propertyName}%"];
            $nav = $common->public_select("community_nav",$navWhere,"id")->select();
            $navWhere = array_column($nav,"id");
            $where['a.nav_id'] = ['in',$navWhere];
        }
        if(!empty($_GET['status'])){
            $status = $_GET['status'];
            $where['a.status'] = $status;
        }
        if(!empty($_GET['colony_id'])){
            $colony_id = $_GET['colony_id'];
            $where['a.colony_id'] = $colony_id;
        }
        if(!empty($_GET['pid'])){
            $pid = $_GET['pid'];
            $where['a.pid'] = $pid;
        }else{
            $pid = 0;
            $where['a.pid'] = 0;
        }

        $thisWechat = Session::get("thisWechat");
        $where['a.uniacid'] = $thisWechat['id'];

        $this->assign("pid",trim($pid,'"'));

        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;

                $config = $common->selconfig();
                $list = Db::connect($config)->name("community_article_classify")->alias("a")
                    ->join("community_colony b","a.colony_id = b.id")
                    ->join("community_nav c","a.nav_id = c.id")
                    ->field("a.*,b.name propertyName,c.name c_name")
                    ->where($where)->limit($start,$limit)->select();
                $type_val = Config::get("model");
                foreach ($list as $k=>$v){
                    $list[$k]['type'] = $type_val[$v['type']];
                }

                $count = Db::connect($config)->name("community_article_classify")->alias("a")
                    ->join("community_colony b","a.colony_id = b.id")
                    ->join("community_nav c","a.nav_id = c.id")
                    ->where($where)->count("*");

                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }

        $assign = [
            "name"=>$name,
            "propertyName"=>$propertyName,
            "status"=>$status,
            "colony_id"=>$colony_id
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }

    //添加分类视图
    public function addClassify(){
        $common = new Common();
        $Together = new Together();
        $community = $Together->findColony();
        $type = Config::get("model");
        $assign = [
            'community'=>$community,
            'type'=>$type
        ];
        $this->assign($assign);
        $pid = empty($_POST['pid'])?0:$_POST['pid'];
        $this->assign("pid",$pid);
        if(empty($_POST['id'])){
            if(empty($community)){
                $nav =[];
            }else{
                //查询栏目
                $nav = $common->public_select("community_nav",['colony_id'=>$community[0]['id']],"id,name")->select();
            }
            $this->assign("nav",$nav);
            return view("addClassify");
        }else{

            $detail = $common->public_select("community_article_classify",['id'=>$_POST['id']])->find();
            //查询栏目
            $nav = $common->public_select("community_nav",['colony_id'=>$detail['colony_id']],"id,name")->select();
            $this->assign("nav",$nav);
            $this->assign("detail",$detail);
            return view("editClassify");
        }
    }

    //添加分类操作
    public function doClass(){
        try{
            $common = new Common();
            if(empty($_POST['pid'])){
                $rule = [
                    ["colony_id", "require", "请选择景区"],
                    ["nav_id", "require", "请选择所属栏目"],
                    ["type", "require", "请选择所属模型"],
                    ["name", "require", "请填写分类名称"],
                ];
            }else{
                $rule = [
                    ["type", "require", "请选择所属模型"],
                    ["name", "require", "请填写分类名称"],
                ];
            }
            $validate = new Validate($rule);
            $value = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $value['uniacid'] = $thisWechat['id'];
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }
            if(empty($_POST['pid'])){
                $colony_id = $value['colony_id'];
            }else{
                $pid = $_POST['pid'];
                $classify = $common->public_select("community_article_classify",['id'=>$pid],"colony_id,nav_id")->find();
                $colony_id = $classify['colony_id'];
                $value['pid'] = $pid;
                $value['colony_id'] = $colony_id;
                $value['nav_id'] = $classify['nav_id'];
            }
            //查询该标签是否已存在
            if(!empty($value['id'])){
                $where['id'] = ['neq',$value['id']];
            }
            $where['name'] = $value['name'];
            $where['nav_id'] = $value['nav_id'];
            $where['colony_id'] = $colony_id;

//            $device = $common->public_select("community_article_classify",$where)->value("id");
//            if(!empty($device)){
//                throw new Exception("该栏目已存在，请勿重复添加");
//            }
            //获取景区名称

            $propertyName = $common->public_select("community_colony",['id'=>$colony_id])->value("name");
            if(empty($value['id'])){
                $value['addtime'] = time();
                $value['add_id'] = Session::get("id");
                $navid = $common->handle("community_article_classify",$value);
                $log['remark'] = "账户【" . Session::get("user_name") . "】为景区【".$propertyName."】添加新栏目【".$value['name']."】,栏目id【".$navid."】" ;
            }else{
                $value['updatetime'] = time();
                $value['update_id'] = Session::get("id");
                $common->handle("community_article_classify",$value);
                $log['remark'] = "账户【" . Session::get("user_name") . "】修改景区【".$propertyName."】栏目【".$value['name']."】,栏目id【".$value['id']."】" ;
            }
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除分类操作
    public function delClass(){
        Db::startTrans();
        try{
            $common = new Common();
            $id = $_POST['id'];
            $navList = $common->public_select("community_article_classify",['id'=>['in',$id]],"colony_id,name")->select();
            $common = new Common();
            foreach ($navList as $k=>$v){
                //获取景区名称
                $propertyName = $common->public_select("community_colony",['id'=>$v['colony_id']])->value("name");
                $log['remark'] = "管理员【" . Session::get("user_name") . "】删除景区【".$propertyName."】分类【".$v['name']."】";
                $log['controller'] = "Article";
                $log['methods'] = "delClass";
                $common->addLog($log);
            }
            $common->public_select("community_article_classify",['id'=>['in',$id]])->delete();
            $common->public_select("community_article_classify",['pid'=>['in',$id]])->delete();
            Db::commit();
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //标签管理
    public function tag(){
        //获取景区列表
        $common = new Common();

        //权限查询
        $role_id = $common->thispower();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);

        $config = $common->selconfig();
        $Together = new Together();
        $community = $Together->findColony();
        $this->assign('community',$community);
        $communityWhere = array_column($community,"id");
        $where['a.colony_id'] = ['in',$communityWhere];
        $name = $colony_id = $status = "";
        if(!empty($_GET['name'])){
            $name = $_GET['name'];
            $where['a.name'] = ['like',"%{$name}%"];
        }
        if(!empty($_GET['colony_id'])){
            $colony_id = $_GET['colony_id'];
            $where['a.colony_id'] = $colony_id;
        }
        if(!empty($_GET['status'])){
            $status = $_GET['status'];
            $where['a.status'] = $status;
        }

        $thisWechat = Session::get("thisWechat");
        $where['a.uniacid'] = $thisWechat['id'];

        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::connect($config)->name("community_article_tag")->alias("a")
                    ->join("community_colony b", "a.colony_id = b.id")
                    ->field("a.*,b.name propertyName")
                    ->where($where)->limit($start,$limit)->select();
                foreach ($list as $k => $v) {

                }

                $count =Db::connect($config)->name("community_article_tag")->alias("a")
                    ->join("community_colony b", "a.colony_id = b.id")
                    ->where($where)->count("*");

                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }


        $assign = [
            "name"=>$name,
            "colony_id"=>$colony_id,
            "status"=>$status
        ];
        $this->assign($assign);
        return $this->fetch('tag');
    }

    //添加标签视图
    public function addTag(){
        $common = new Common();
        $Together = new Together();
        $community = $Together->findColony();
        $assign = [
            'community'=>$community,
        ];
        $this->assign($assign);
        if(empty($_POST['id'])){
            return view("addTag");
        }else{
            $detail = $common->public_select("community_article_tag",['id'=>$_POST['id']])->find();
            $this->assign("detail",$detail);
            return view("editTag");
        }
    }

    //删除标签
    public function delTag(){
        $common = new Common();
        Db::startTrans();
        try{
            $id = $_POST['id'];
            $navList = $common->public_select("community_article_tag",['id'=>['in',$id]],"colony_id,name")->select();
            foreach ($navList as $k=>$v){
                //获取小区名称
                $propertyName = $common->public_select("community_colony",['id'=>$v['colony_id']])->value("name");
                $log['remark'] = "管理员【" . Session::get("user_name") . "】删除景区【".$propertyName."】标签【".$v['name']."】";
                $log['controller'] = "Article";
                $log['methods'] = "delTag";
                $common->addLog($log);
            }
            $common->public_select("community_article_tag",['id'=>['in',$id]])->delete();
            Db::commit();
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //标签执行添加操作
    public function doTag(){
        try{
            $common = new Common();
            $rule = [
                ["colony_id", "require", "请选择小区"],
                ["name", "require", "请填写标签名称"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $value['uniacid'] = $thisWechat['id'];
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }
            //查询该标签是否已存在
            if(!empty($value['id'])){
                $where['id'] = ['neq',$value['id']];
            }
            $where['name'] = $value['name'];
            $where['colony_id'] = $value['colony_id'];

            $device =  $common->public_select("community_article_tag",$where)->value("id");
            if(!empty($device)){
                throw new Exception("该标签已存在，请勿重复添加");
            }
            //获取景区名称
            $propertyName = $common->public_select("community_colony",['id'=>$value['colony_id']])->value("name");
            if(empty($value['id'])){
                $navid = $common->handle("community_article_tag",$value);
                $log['remark'] = "账户【" . Session::get("user_name") . "】为景区【".$propertyName."】添加新标签【".$value['name']."】,标签id【".$navid."】" ;
            }else{
                $common->handle("community_article_tag",$value);
                $log['remark'] = "账户【" . Session::get("user_name") . "】修改景区【".$propertyName."】标签【".$value['name']."】,标签id【".$value['id']."】" ;
            }
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


}